package com.tao.user.controller;


import com.tao.user.domain.dto.LoginDto;
import com.tao.user.domain.dto.PageSelectUserDto;
import com.tao.user.domain.dto.UserDto;
import com.tao.user.domain.pojo.User;
import com.tao.result.Result;
import com.tao.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/webPage/user")
public class UserController {

    @Autowired
    UserService userService;




    @PostMapping("/login")
    public Result loginController(@RequestBody LoginDto loginDto) {
        log.info("用户{}登录", loginDto.getName());
        return Result.success(userService.loginService(loginDto));
    }

    @PostMapping("/signup")
    public Result signup(@RequestBody UserDto userDto){
        log.info("Controller层接收到前端数据:{}", userDto.getName());
        userService.userAddService(userDto);
        return Result.success();
    }

    @PostMapping("/add")
    public Result addUserControler(@RequestBody UserDto userDto) {
        log.info("Controller层接收到前端数据:{}", userDto.getName());
        userService.userAddService(userDto);
        return Result.success();
    }

    @GetMapping("/pageSelect")
    public Result pageSelectControler(PageSelectUserDto pageSelectUserDto) {
        log.info("接收到前端传来的参数,第{}页,每页{}条", pageSelectUserDto.getPage(), pageSelectUserDto.getPageSize());
        return Result.success(userService.pageSelectService(pageSelectUserDto));
    }

    @PutMapping("/update")
    public Result updateUserController(@RequestBody UserDto userDto) {
        log.info("接收到前端传来的参数,UserDto:{}", userDto);
        userService.updateUserService(userDto);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getUserByIdController(@PathVariable Integer id) {
        log.info("接收到前端传来的参数,查询id=:{}的用户", id);
        User user = userService.getUserByIdService(id);
        return Result.success(user);
    }

    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, Integer id) {
        log.info("Controller接收到前端参数，需要将用户{}状态改为：{}", id, status);
        userService.updateStatus(status, id);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result deleteUser(@RequestParam List<Integer> ids) {
        userService.deleteUser(ids);
        return Result.success();
    }
}
